package com.thatvn.diemtinvietnam;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.text.Html;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import com.thatvn.diemtinvietnam.ImageLoader;
import com.thatvn.diemtinvietnam.R;
import com.thatvn.diemtinvietnam.Utils;
public class TrendAdapter extends BaseExpandableListAdapter {
	private Activity activity;
	private ArrayList<Object> childtems;
	private LayoutInflater inflater;
	private ArrayList<HashMap<String, String>> parentItems, child;
	private HashMap<String, String> parentNews;
	public ImageLoader imageLoader;
	public Typeface typefaceTitle,typefaceInfo;
	public Context context;
	private static final int[] EMPTY_STATE_SET = {};
    private static final int[] GROUP_EXPANDED_STATE_SET =
            {android.R.attr.state_expanded};
    private static final int[][] GROUP_STATE_SETS = {
         EMPTY_STATE_SET, // 0
         GROUP_EXPANDED_STATE_SET // 1
	};
    
	public TrendAdapter(ArrayList<HashMap<String, String>> parents, ArrayList<Object> childern,Activity a,Context context) {
		this.parentItems = parents;
		this.childtems = childern;
		this.context=context;
		activity=a;
		imageLoader=new ImageLoader(activity.getApplicationContext());
		typefaceTitle=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Black.ttf");
	    typefaceInfo=Typeface.createFromAsset(context.getAssets(),"fonts/Roboto-Regular.ttf");
	}

	public void setInflater(LayoutInflater inflater, Activity activity) {
		this.inflater = inflater;
		this.activity = activity;
	}

	
	@Override
	public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		//Log.e("BBBBBBBBBBBBBBBBgroupPosition="+groupPosition, "childPosition="+childPosition);
//		if (isLastChild && childPosition==0){
//			//Load tin chinh
////			titleRelated = (TextView) convertView.findViewById(R.id.titleRelated);
////	        sourceRelated = (TextView) convertView.findViewById(R.id.sourceRelated);
////			thumb_image=(ImageView) convertView.findViewById(R.id.list_image_related);
//			Log.e("getChildrenCountgetChildrenCountgetChildrenCount", String.valueOf(getChildrenCount(groupPosition)));
//			//RelativeLayout rl=(RelativeLayout) convertView.findViewById(R.id.rltLayout);
//			//rl.setVisibility(View.GONE);
//			Intent vi = new Intent(activity, viewWeb.class);
//			vi.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//			//Intent vi = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
//			vi.putExtra("link", this.parentItems.get(groupPosition).get(MainActivity.KEY_LINK));			
//			vi.putExtra("id", this.parentItems.get(groupPosition).get(MainActivity.KEY_ID));
//			//vi.setData(Uri.parse(link));
//			context.startActivity(vi);
//			if (convertView == null) {				
//					convertView = inflater.inflate(R.layout.list_row_child_emty, null);				
//			}
//			return convertView;
//		}
		TextView name = null,maindomain=null;
		ImageView thumb_image=null;
//		if (groupPosition>=childtems.size()){
//			Log.e("BBBBBBBBBBBBBBBB", "Not FOUNDDDDDDDDDDDDDDDDDD");
//			return null;
//		}
		child = (ArrayList<HashMap<String,String>>) this.childtems.get(groupPosition);

		
		if (convertView == null) {			
				convertView = inflater.inflate(R.layout.trend_list_row_child, null);
		}
		HashMap<String, String> news = new HashMap<String, String>();
		
		if (childPosition<child.size()){
	        news = child.get(childPosition);
	        if (news.get("name")!=null && !news.get("name").isEmpty()){
		        name = (TextView) convertView.findViewById(R.id.name);	        
		        name.setText(Html.fromHtml(news.get("name")));
		        maindomain = (TextView) convertView.findViewById(R.id.maindomain);	        
		        maindomain.setText(Utils.smoothDomain(news.get("maindomain")));
				thumb_image=(ImageView) convertView.findViewById(R.id.list_image);
				
				if (Config.isTablet) {
					name.setTextSize(context.getResources().getDimension(R.dimen.TableTextSizeTitle));
					maindomain.setTextSize(context.getResources().getDimension(R.dimen.TableTextSizeNormal));	        	
		        	thumb_image.getLayoutParams().height = 120;
		        	thumb_image.getLayoutParams().width = 120;
		        }
				//Log.e("aaaaaaaaaaaaaaaaaaa,child size="+child.size(), String.valueOf(news.get(MainActivity.KEY_NAMERELATED)));
			//}
				String image="";
		        try{
		        	image=news.get("image").trim();
		        if (image.isEmpty() || image.equals("") || image.equals(null)) 
		        	{
		        		//Log.e("getImage", String.valueOf(news.get(MainActivity.KEY_SOURCE)));
		        		image=Utils.getDomainImage(news.get("maindomain"));
		        		//Log.e("image=", String.valueOf(image));
		        	}
		        }catch(Exception ex){
		        		image=Utils.getDomainImage(news.get("maindomain"));
		        }
		        imageLoader.DisplayImage(image, thumb_image);
	        }
		}    
		
		return convertView;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		View vi=convertView;
		if (convertView == null) {
			vi = inflater.inflate(R.layout.trend_list_row, null);
		}
		//Log.e("getChildrenCountgetChildrenCountgetChildrenCount", String.valueOf(getChildrenCount(groupPosition)));
		View ind = vi.findViewById( R.id.explist_indicator);
		if( ind != null ) {
			ImageView indicator = (ImageView)ind;
			if( getChildrenCount(groupPosition) <= 0 ) {
				indicator.setVisibility(View.INVISIBLE);
			} else {
				indicator.setVisibility(View.VISIBLE);
				int stateSetIndex = ( isExpanded ? 1 : 0);
				Drawable drawable = indicator.getDrawable();
				drawable.setState(GROUP_STATE_SETS[stateSetIndex]);
			}
		}
		
//		((CheckedTextView) convertView).setText(parentItems.get(groupPosition));
//		((CheckedTextView) convertView).setChecked(isExpanded);
		    //ImageView imageMain = (ImageView)vi.findViewById(R.id.imageMain); 
		    TextView keyword = (TextView)vi.findViewById(R.id.keyword); // title
	        TextView no = (TextView)vi.findViewById(R.id.no); // source
	        
	        if (Config.isTablet) {
	        	keyword.setTextSize(context.getResources().getDimension(R.dimen.TableTextSizeTitle));
	        	no.setTextSize(context.getResources().getDimension(R.dimen.TableTextSizeTitle));
	        }
	        
	        HashMap<String, String> news = new HashMap<String, String>();
	        news = this.parentItems.get(groupPosition);
	        //AssetManager am=getContext(;
	        // Setting all values in listview
	        
	        keyword.setTypeface(typefaceTitle);
	        no.setTypeface(typefaceTitle);
	        String skeyword="";
	        if (!news.get("title").isEmpty()) 
	        	skeyword=news.get("title");
	        else
	        	skeyword=news.get("keyword");
	        
	        keyword.setText(Html.fromHtml(skeyword));
	        no.setText(String.valueOf(groupPosition+1)+". ");
//	        String image="";
//	        try{
//	        	image=news.get(MainActivity.KEY_IMAGE).trim();
////	        	if (image.isEmpty() || image.equals("") || image.equals(null)) 
////	        	{
////	        		//Log.e("getImage", String.valueOf(news.get(MainActivity.KEY_SOURCE)));
////	        		image=Utils.getDomainImage("");
////	        		//Log.e("image=", String.valueOf(image));
////	        	}
//	        }catch(Exception ex){
//	        		image="";//Utils.getDomainImage("");
//	        }
//	        if (!Utils.isNotImage(image)){
//	        	imageLoader.DisplayImage(image, imageMain);
//	        }else{
//	        	imageMain.setVisibility(View.GONE);
//	        }	        
	        
		return vi;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return null;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return 0;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		try{
		child = (ArrayList<HashMap<String,String>>) childtems.get(groupPosition);
		//if (child.get(0).isEmpty() || child.get(0).get("name").equals("")) return 0;
		return child.size();
		}catch(Exception ex){
			return 0;
		}
		//return childtems.get(groupPosition)..size();
		
	}

	@Override
	public Object getGroup(int groupPosition) {
		return null;
	}

	@Override
	public int getGroupCount() {
		return parentItems.size();
	}

	@Override
	public void onGroupCollapsed(int groupPosition) {
		super.onGroupCollapsed(groupPosition);
	}

	@Override
	public void onGroupExpanded(int groupPosition) {
		super.onGroupExpanded(groupPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return 0;
	}

	

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
    
}